package com.smarttest.common.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Collection Utils
 */
public class CollectionUtils {

    /**
     * 将数组添加进列表
     *
     * @param string
     * @return
     */
    public static List arrayToList(String... string) {
        List<String> lst = new ArrayList<String>();
        for (String st : string) {
            lst.add(st);
        }
        return lst;
    }

    /**
     * 把List转换为:('1','2','3')格式字符串
     *
     * @param poolList
     *            要转换的格式
     * @param key
     *            map中的key
     * @return
     */
    public static String getInStr(List<Map> poolList, String key) {
        StringBuffer poolId = new StringBuffer();
        if (poolList != null && poolList.size() > 0) {
            poolId.append("(");
            for (Map pool : poolList) {
                if (!poolId.toString().equals("(")) {
                    poolId.append(",");
                }
                poolId.append("'" + pool.get(key) + "'");
            }
            poolId.append(")");
        }
        if (poolId.length() == 0) {
            poolId.append("('')");
        }
        return poolId.toString();
    }

    /**
     * 组合一组对象为Map, 偶数位对象为键, 奇数位对象为值.
     *
     * @param objects
     *            对象列表。
     * @return Map实例
     */
    public static Map asMap(Object... objects) {
        Map m = new HashMap(objects.length / 2 + 1);
        int i = 0;
        for (; i < objects.length - 1; i += 2) {
            m.put(objects[i], objects[i + 1]);
        }

        if (i < objects.length) {
            m.put(objects[i], null);
        }

        return m;
    }

    /**
     * 填充Map中指定key的默认值
     *
     * @param inMap
     * @param key
     * @param defaultValue
     * @return
     */
    public static Map fillValueByKey(Map map, String key, String defaultValue) {
        if (null == map.get(key)) {
            map.put(key, defaultValue);
        }
        return map;
    }

    /**
     * 判断List中是否有元素
     *
     * @param obj
     * @return
     */
    public static boolean listHasElement(Object obj) {
        List list = (List) obj;
        if (list.size() >= 1) {
            return true;
        }
        return false;

    }

}
